package org.example.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.entity.Member;
import org.example.mapper.MemberMapper;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class MemberService extends ServiceImpl<MemberMapper, Member> implements IMemberService {


    public Member getMemberById(Long id) {

        return this.getById(id);
    }

    public List<Member> getMemberAll() {

        List<Member> list1 = this.list();

        if (CollUtil.isEmpty(list1)) {
            return ListUtil.empty();
        }

        list1.forEach(System.out::println);
        // 业务逻辑  批量更改年   +1  price * 3

        return list1;
    }

    public boolean saveMember(Member member) {
        return this.save(member);
    }

    public boolean updateMember(Member member) {
        return this.updateById(member);
    }

    public boolean removeMemberById(Long id) {

        return this.removeById(id);
    }

    public Member getMemberByName(String name) {
        return this.getOne(Wrappers.<Member>lambdaQuery().likeLeft(Member::getName, name));
    }


    public BigDecimal getMemberPriceById(Long id) {
        return this.getById(id).getPrice();
    }

    //充值
    public boolean recharge(Long id, BigDecimal price) {
        Member member = this.getById(id);
        member.setPrice(member.getPrice().add(price));
        return this.updateById(member);
    }

/*    public List<Member> getAllMember() {
        return this.list();
    }*/
}
